﻿using System.ComponentModel;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PrismFactory.SoftwareFactory.Views.CreateSolution;
using Rhino.Mocks;

namespace PrismGuidancePackage.Views.Tests
{
    [TestClass]
    public class CollectRootNamespacePageViewModelFixture
    {
        private CollectRootNamespacePageViewModel viewModel;
        private IDictionaryService stubDictionaryService;

        [TestInitialize]
        public void Setup()
        {
            stubDictionaryService = MockRepository.GenerateStub<IDictionaryService>();
            viewModel = new CollectRootNamespacePageViewModel(stubDictionaryService);
        }

        [TestMethod]
        public void ValidWhenRootNamespaceIsNotNull()
        {
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return("MyRoot.MyChild");
            Assert.IsNull((viewModel as IDataErrorInfo)[CollectRootNamespacePageViewModel.RootNamespaceKey]);
        }

        [TestMethod]
        public void InvalidWhenRootNamespaceIsNull()
        {
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return(null);
            Assert.IsNotNull((viewModel as IDataErrorInfo)[CollectRootNamespacePageViewModel.RootNamespaceKey]);
        }

        [TestMethod]
        public void InvalidWhenRootNamespaceIsEmpty()
        {
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return("");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[CollectRootNamespacePageViewModel.RootNamespaceKey]);
        }

        [TestMethod]
        public void InvalidWhenRootNamespaceIsNotProperlyFormed1()
        {
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return("Invalid Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[CollectRootNamespacePageViewModel.RootNamespaceKey]);
        }

        [TestMethod]
        public void InvalidWhenRootNamespaceIsNotProperlyFormed3()
        {
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return("Invalid%Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[CollectRootNamespacePageViewModel.RootNamespaceKey]);
        }

        [TestMethod]
        public void InvalidWhenRootNamespaceIsNotProperlyFormed4()
        {
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return("1Invalid.Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[CollectRootNamespacePageViewModel.RootNamespaceKey]);
        }

        [TestMethod]
        public void SetsProjectNamesBasedOnRootNamespaceValue()
        {
            const string rootNamespace = "MyRoot";
            stubDictionaryService.Stub(s => s.GetValue(CollectRootNamespacePageViewModel.RootNamespaceKey)).Return(rootNamespace);
            viewModel.RootNamespace = rootNamespace;
            stubDictionaryService.AssertWasCalled(s => s.SetValue(CollectRootNamespacePageViewModel.ShellProjectNameKey, rootNamespace + ".Shell"));
            stubDictionaryService.AssertWasCalled(s => s.SetValue(CollectRootNamespacePageViewModel.InfrastructureProjectNameKey, rootNamespace + ".Infrastructure"));
            stubDictionaryService.AssertWasCalled(s => s.SetValue(CollectRootNamespacePageViewModel.ShellTestProjectNameKey, rootNamespace + ".Shell.Tests"));
            stubDictionaryService.AssertWasCalled(s => s.SetValue(CollectRootNamespacePageViewModel.InfrastructureTestProjectNameKey, rootNamespace + ".Infrastructure.Tests"));
        }
    }
}